<?php

namespace app\api\model;

use think\Model;
use think\Db;
use app\api\model\Config as ConfigModel;
/**
 * 用户模型
 */
class User extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [

    ];

    /**获取用户级别
     * @param $uid
     * 获取商品级别
     * 判断是否为普通还是安装店,代理,一级代理
     * $is_vip 会员级别 0 = 普通用户 1 = 安装店 2 = 代理商 3 = 一级代理
     */
    public function getUserInfo($uid = 0)
    {
        $result = [
            'data' => [],
            'status' => true,
            'msg'   => ''
        ];
        $user = Db::name('user')->where('id',$uid)->find();

        if (empty($user))
        {
            $result['is_vip'] = 0;
            $result['status'] = false;
            return $result;
        }
        //普通用户
        if ($user['condition'] == 0)
        {
            $result['data'] = $user;
            $result['is_vip'] = 0;
            $result['status'] = false;
            return $result;
        }
        //安装店
        if ($user['condition'] == 1 && $user['is_status'] != 1)
        {
            $result['data'] = $user;
            $result['is_vip'] = 1;
            return $result;
        }
        //一级代理
        if ($user['condition'] == 2 && $user['is_oneagent'] == 1 && $user['is_status'] != 1 )
        {
            $result['data'] = $user;
            $result['is_vip'] = 3;
            return $result;
        }
        //代理商
        if ($user['condition'] == 2 && $user['is_status'] != 1)
        {
            $result['data'] = $user;
            $result['is_vip'] = 2;
            return $result;
        }

        $result['data'] = $user;
        $result['is_vip'] = 0;
        return $result;
    }
    /**
     * 获取用户信息
     */
    public function getUser($uid)
    {
        $res = Db::name('user')->field('id,username,nickname,singtime,mobile,avatar,level,gender,money,score,successions,token,
                    status,yid,condition,is_oneagent,is_status,province,city,models,price_sum,number,unit_number,is_price,address_id
                    is_admin,openid,is_sign,cartype')->where('id',$uid)->find();
        if (!$res) return false;
        return $res;
    }
    /**
     * 修改信息
     */
    public function updateUser($uid,$data = [])
    {

        $data['updatetime']  = time();
        $res = Db::name('user')->where('id',$uid)->update($data);
        if (!$res) return false;
        return $res;
    }

    /**
     * @param array $userInfo
     * @param $content
     * @return false|int|string
     * 反馈信息
     * $content 反馈内容
     * $userInfo 用户新
     */
    public function getFeedback($userInfo = [],$content)
    {
        if (!$userInfo) return false;
        $data = [
            'uid' => $userInfo['id'],
            'name' => $userInfo['nickname'],
            'content' => $content,
            'create_time'   => time()
        ];
        $res = Db::name('user_feedback')->insert($data);
        return $res;
    }

    /**
     *最后一次签到时间
     */
    public function getSing($uid)
    {

        // 查询到用户最后一次签到时间
        $sign = Db::name('sign')->where('user_id',$uid)->order('last_sign_time desc')->limit(1)->find();
        return $sign;
    }

    /**
     * @param $uid  用户ID
     * @param $time 签到时间
     * @return false|int|string
     */
    public function signAdd($uid, $time, $integral = 0)
    {
        $data = [
            'user_id'     => $uid,
            'integral'    => $integral,
            'create_time' => $time
        ];

        $response = Db::name('sign')->insertGetId($data);

        if (!$response) return false;

        return $response;
    }

    /**
     * @param array $user 分享用户信息
     * @param array $goods 商品信息
     * @param string $uid 用户id
     * @param string $order_id 订单id
     * @param string $type 类型
     * @return false
     * 奖励
     */
    public function usersell($user = [], $goods = [],$uid,$order_id, $type = 0)
    {
        if (empty($user) || empty($goods))
        {
            return false;
        }
        $ConfigModel = new ConfigModel();
        $angent      = $ConfigModel->getAgent(); //普通代理商
        $OneAgent    = $ConfigModel->getOneAgent(); //超级代理商


        foreach($goods as $key => $val){
            if ($user['is_vip'] == 0)
            {
                $goods[$key]['y_price'] = $user['ordinaryprice'];
            }elseif ($user['is_vip'] == 1){
                $goods[$key]['y_price'] = $val['haveprice'];
            }elseif ($user['is_vip'] == 2){
                $goods[$key]['y_price'] = $val['agentprice'] != 0 ? $val['agentprice'] : bcmul($val['haveprice'], $angent, 2);
            }elseif ($user['is_vip'] == 3){
                $goods[$key]['y_price'] = $val['agentprice'] != 0 ? $val['agentprice'] : bcmul($val['haveprice'], $OneAgent, 2);
            }
            $goods[$key]['y_price'] = bcsub($val['price'],$goods[$key]['y_price']);
//            $goods[$key]['y_price'] = bcmul($goods[$key]['y_price'], $val['num']);
            $share_data = [
                'share_id' => $user['data']['id'],
                'uid' => $uid,
                'gid' => $val['id'],
                'share_integral' => $goods[$key]['y_price'] == 0 ?: $goods[$key]['y_price'],
                'create_time' => time(),
                'order_id'    => $order_id,
                'type'        => $type,
            ];

            $response = Db::name('user_share')->insertGetId($share_data);
        }
        if ($response)
        {
            return true;
        }else{
            return false;
        }

    }

    /**
     * 计算商品价格
     */
    public static function goodsprice($uid = '',$list = [])
    {
        $userInfo = (new User())->getUserInfo($uid);
        $Config = new Config ();
        $ageent = $Config->getAgent();
        $OneAgent = $Config->getOneAgent();
        foreach($list as $key => $val)
        {
            if ($userInfo['is_vip'] == 0)
            {
                $price = $val['ordinaryprice'];
            }
            if ($userInfo['is_vip'] == 1)
            {
                $price =  empty($val['agentprice']) ||  $val['agentprice'] == 0 ? $val['haveprice'] : $val['agentprice'];
            }
            if ($userInfo['is_vip'] == 2)
            {
                if ($userInfo['data']['is_price'] == 1)
                {
                    $price = empty($val['agentprice']) ||  $val['agentprice'] == 0 ? bcmul($ageent, $val['haveprice'], 2) : $val['agentprice'];
                }else{
                    $price =  empty($val['agentprice']) ||  $val['agentprice'] == 0 ? $val['haveprice'] : $val['agentprice'];
                }
            }
            if ($userInfo['is_vip'] == 3)
            {
                if ($userInfo['data']['is_price'] == 1)
                {
                    $price = empty($val['agentprice']) ||  $val['agentprice'] == 0? bcmul($OneAgent, $val['haveprice'], 2) : $val['agentprice'];
                }else{
                    $price =  empty($val['agentprice']) ||  $val['agentprice'] == 0 ? $val['haveprice'] : $val['agentprice'];
                }
            }
            $list[$key]['price'] = $price;
        }

        return $list;
    }

    public static function userCar($carId = 0)
    {
        $userCar = Db::name('user_car')
            ->field('brand,engine,models,label,year')
            ->where('id',$carId)
            ->find();

        return $userCar;
    }


}
